<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>query传参</title>
</head>
<body>
    <h1>get请求-query传参</h1>
    <form action="" method="" id = "form">
        <input type="text" id = "ipt">
        <button id = "btn">发送请求</button>
    </form>

    <script>
        const oBtn = document.getElementById('btn')
        const oIpt = document.getElementById('ipt')
        const form = document.getElementById('form')

        form.onsubmit = function(){
            //获取用户输入的userId
            const userId = oIpt.value;

            //创建xhr实例
            const xhr = new XMLHttpRequest();
            //打开路径
            xhr.open("get",`/user?userId=${userId}&_=${Date.now()}`,true)
            //发送请求
            xhr.send()
            //监听事件
            xhr.onreadystatechange = function () {
                //当状态码为4,响应码为2xx的时候说明请求和响应都成功
                if (xhr.readyState === 4 && (xhr.status >= 200 && xhr.status <= 299)) {
                const data = JSON.parse(xhr.responseText)
                console.log(data);
                }
            }

            //阻止默认行为,防止提交的事后网页刷新
            return false
        }
    </script>
</body>
</html>